using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.Drawing;
using System.Drawing.Drawing2D;

namespace 水库上位机
{
    public class USwith : Control
    {
        private bool isOn = false;
        private Color onColor = Color.Green;
        private Color offColor = Color.Red;
        private string onText = "开启";
        private string offText = "关闭";

        [Category("行为")]
        [Description("开关状态")]
        public bool IsOn
        {
            get { return isOn; }
            set 
            {
                isOn = value;
                this.Invalidate();
            }
        }

        [Category("外观")]
        [Description("开启颜色")]
        public Color OnColor
        {
            get { return onColor; }
            set 
            {
                onColor = value;
                this.Invalidate();
            }
        }

        [Category("外观")]
        [Description("关闭颜色")]
        public Color OffColor
        {
            get { return offColor; }
            set 
            {
                offColor = value;
                this.Invalidate();
            }
        }

        [Category("外观")]
        [Description("开启文本")]
        public string OnText
        {
            get { return onText; }
            set 
            {
                onText = value;
                this.Invalidate();
            }
        }

        [Category("外观")]
        [Description("关闭文本")]
        public string OffText
        {
            get { return offText; }
            set 
            {
                offText = value;
                this.Invalidate();
            }
        }

        public USwith()
        {
            this.DoubleBuffered = true;
            this.ResizeRedraw = true;
            this.Size = new Size(60, 25);
            this.Click += USwith_Click;
        }

        private void USwith_Click(object sender, EventArgs e)
        {
            isOn = !isOn;
            this.Invalidate();
        }

        protected override void OnPaint(PaintEventArgs e)
        {
            base.OnPaint(e);
            Graphics g = e.Graphics;
            g.SmoothingMode = SmoothingMode.AntiAlias;

            // 绘制开关背景
            using (Brush brush = new SolidBrush(isOn ? onColor : offColor))
            {
                g.FillRoundedRectangle(brush, new Rectangle(0, 0, this.Width - 1, this.Height - 1), 4);
            }
            using (Pen pen = new Pen(Color.Black, 1))
            {
                g.DrawRoundedRectangle(pen, new Rectangle(0, 0, this.Width - 1, this.Height - 1), 4);
            }

            // 绘制文本
            using (Font font = new Font("微软雅黑", 8F, FontStyle.Bold))
            {
                using (Brush brush = new SolidBrush(Color.White))
                {
                    string text = isOn ? onText : offText;
                    SizeF textSize = g.MeasureString(text, font);
                    g.DrawString(text, font, brush, (this.Width - textSize.Width) / 2, (this.Height - textSize.Height) / 2);
                }
            }
        }
    }

    public static class GraphicsExtensions
    {
        public static void FillRoundedRectangle(this Graphics g, Brush brush, Rectangle rect, int radius)
        {
            using (GraphicsPath path = GetRoundedRectanglePath(rect, radius))
            {
                g.FillPath(brush, path);
            }
        }

        public static void DrawRoundedRectangle(this Graphics g, Pen pen, Rectangle rect, int radius)
        {
            using (GraphicsPath path = GetRoundedRectanglePath(rect, radius))
            {
                g.DrawPath(pen, path);
            }
        }

        private static GraphicsPath GetRoundedRectanglePath(Rectangle rect, int radius)
        {
            GraphicsPath path = new GraphicsPath();
            int diameter = radius * 2;

            // 左上圆角
            path.AddArc(rect.X, rect.Y, diameter, diameter, 180, 90);
            // 右上圆角
            path.AddArc(rect.Right - diameter, rect.Y, diameter, diameter, 270, 90);
            // 右下圆角
            path.AddArc(rect.Right - diameter, rect.Bottom - diameter, diameter, diameter, 0, 90);
            // 左下圆角
            path.AddArc(rect.X, rect.Bottom - diameter, diameter, diameter, 90, 90);
            path.CloseFigure();

            return path;
        }
    }
}